<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */
    
    /*********************************************************
    *            --== InArch ==--
    *
    * Ajax Drop Page
    *
    * @author Thomas Brandstätter <tb.essentialmind.com>
    * @version n/a
    * @copyright essentialmind.com 2010
    *
    *********************************************************/

    /***
    * class ajaxCopyPage
    ****
    * This class will insert an new node based of an origin and duplicate
    * the associated content
    ****
    * @package ajax
    * @author A Kulikov <a.kulikov@gmail.com>
    * @author Thomas Brandstätter <tb.essentialmind.com>
    * @version n/a
    * @copyright essentialmind.com 2010
    ***/
    class ajaxCopyPage extends ajaxAction{
        
        
        public function process(){
            $id = (int)$_REQUEST['id'];
            
            //try to copy the page
            try{
	
	
                //fetch origin row
                $origin = $this->db->getRow("SELECT pm.*, dt.codeset as dtcode FROM pagemap as pm
                                                LEFT JOIN datatype AS dt ON pm.pagetype = dt.id
                                                WHERE pm.id = " . $id);
                $newID = -1;
                
                //fetch preceed of associates
                if      ($origin['dtcode'] == "page") {  
                    
                    
                    
                        // fetch origin page
                        $originPage = $this->db->getRow("SELECT * FROM node_page WHERE id = ".$origin['pageid']);
                        
                        // fetch next sequence value 
                        $nextID = $this->db->getOne("SELECT nextval('node_page_id_seq')");
                        
                        // insert new Page
                        $this->db->Execute("INSERT INTO node_page(id, content) VALUES(" . $nextID . ", '" . $originPage['content'] . "')");
                        // return new ID
                        $newID = $this->db->getOne("SELECT last_value FROM node_page_id_seq");
                    
                    
                } 
                elseif  ($origin['dtcode'] == "inserat") {  
                    
                    
                        // fetch origin
                        $originInserat = $this->db->getRow("SELECT * FROM node_inserat WHERE id = ".$origin['pageid']);


                        // text_field1, text_field2, text_field3, text_field4, text_field5, text_field6, text_field7, text_field8, text_field9, text_field10, 
                        // bool_field1, bool_field2, bool_field3, bool_field4, bool_field5, bool_field6, bool_field7, bool_field8, bool_field9, bool_field10, 
                        // float_field1, float_field2, float_field3, float_field4, float_field5, float_field6, float_field7, float_field8, float_field9, float_field10, 
                        // int_field1, int_field2, int_field3, int_field4, int_field5, int_field6, int_field7, int_field8, int_field9, int_field10, 


                        // '" . $originInserat['text_field1'] . "','" . $originInserat['text_field2'] . "', '" . $originInserat['text_field3'] . "', '" . $originInserat['text_field4'] . "', '" . $originInserat['text_field5'] . "', '" . $originInserat['text_field6'] . "', '" . $originInserat['text_field7'] . "', '" . $originInserat['text_field8'] . "', '" . $originInserat['text_field9'] . "', '" . $originInserat['text_field10'] . "', 
                        // " . $originInserat['bool_field1'] . "," . $originInserat['bool_field2'] . ", " . $originInserat['bool_field3'] . ", " . $originInserat['bool_field4'] . ", " . $originInserat['bool_field5'] . ", " . $originInserat['bool_field6'] . ", " . $originInserat['bool_field7'] . ", " . $originInserat['bool_field8'] . ", " . $originInserat['bool_field9'] . ", " . $originInserat['bool_field10'] . ", 
                        // " . $originInserat['float_field1'] . "," . $originInserat['float_field2'] . ", " . $originInserat['float_field3'] . ", " . $originInserat['float_field4'] . ", " . $originInserat['float_field5'] . ", " . $originInserat['float_field6'] . ", " . $originInserat['float_field7'] . ", " . $originInserat['float_field8'] . ", " . $originInserat['float_field9'] . ", " . $originInserat['float_field10'] . ",
                        // " . $originInserat['int_field1'] . "," . $originInserat['int_field2'] . ", " . $originInserat['int_field3'] . ", " . $originInserat['int_field4'] . ", " . $originInserat['int_field5'] . ", " . $originInserat['int_field6'] . ", " . $originInserat['int_field7'] . ", " . $originInserat['int_field8'] . ", " . $originInserat['int_field9'] . ", " . $originInserat['int_field10'] . ",
                        


                        // insert new inserat
                        // we leave the columns visits, questions, feature_root and 
                        // feature_cat blank
                        $this->db->Execute("INSERT INTO node_inserat(title, 
                                                                            \"content\", 
                                                                            owner_mail, 
                                                                            owner_code, 
                                                                            image, 
                                                                            ins_type, 
                                                                            price_type, 
                                                                            price, 
                                                                            confirmed, 
                                                                            notified, 
                                                                            section, 
                                                                            address, 
                                                                            city, 
                                                                            zip, 
                                                                            country, 
                                                                            longitude, 
                                                                            latitude, 
                                                                            altitude, 
                                                                            itype, 
                                                                            visits, 
                                                                            questions, 
                                                                            ip, 
                                                                            blacklist, 
                                                                            feature_root, 
                                                                            feature_cat
                                                                ) VALUES (  '" . $originInserat['title'] ."', 
                                                                            '" . $originInserat['content'] ."', 
                                                                            '" . $originInserat['owner_mail'] ."', 
                                                                            '" . $originInserat['owner_code'] ."', 
                                                                            '" . $originInserat['image'] ."', 
                                                                            " . $originInserat['ins_type'] .", 
                                                                            " . $originInserat['price_type'] .", 
                                                                            " . $originInserat['price'] .", 
                                                                            '" . $originInserat['confirmed'] ."', 
                                                                            '" . $originInserat['notified'] ."', 
                                                                            " . $originInserat['section'] .", 
                                                                            '" . $originInserat['address'] ."', 
                                                                            '" . $originInserat['city'] ."', 
                                                                            '" . $originInserat['zip'] ."', 
                                                                            '" . $originInserat['country'] ."', 
                                                                            '" . $originInserat['longitude'] ."', 
                                                                            '" . $originInserat['latitude'] ."', 
                                                                            '" . $originInserat['altitude'] ."', 
                                                                            " . $originInserat['itype'] .", 
                                                                            0, 
                                                                            0, 
                                                                            " . $originInserat['ip'] . ", 
                                                                            '" . $originInserat['blacklist'] . "', 
                                                                            'f', 'f')" );

                        //return new ID
                        $newID = $this->db->getOne("SELECT last_value FROM node_inserat_id_seq");
                    
                }

                $newURL = $origin['pageurl']."-copy";
                $isPresent = $this->db->getOne("SELECT * FROM pagemap 
                                                WHERE nodeid = " .(int)$origin['nodeid'] . " AND pageurl = '" . $newURL . "'" );
                
                if ($isPresent != nil) {
                    $newURL = $newURL . time();
                }

                // insert node into pagemap and leave pageurl blank
				if ($GLOBALS['_CONFIG']['enable_order_position']) {
				
			        $nextPos = $this->db->getOne("SELECT max(order_pos) FROM pagemap ");
            	    $nextPos++;
				
                	$this->db->Execute("INSERT INTO pagemap(nodeid,pageurl,pageid,pagetype,title, owner_gid, owner_uid, order_pos) VALUES(
	                                                                                '".(int)$origin['nodeid']."',
	                                                                                '".$newURL."',
	                                                                                '".(int)$newID."',
	                                                                                '".(int)$origin['pagetype']."',
	                                                                                '".$origin['title']."-Copy',
	                                                                                ".$origin['owner_gid'].",
	                                                                                ".$origin['owner_uid'].",
	                																".$nextPos."
	                                                                            )");
			    } else {
				
					$this->db->Execute("INSERT INTO pagemap(nodeid,pageurl,pageid,pagetype,title, owner_gid, owner_uid) VALUES(
	                                                                                '".(int)$origin['nodeid']."',
	                                                                                '".$newURL."',
	                                                                                '".(int)$newID."',
	                                                                                '".(int)$origin['pagetype']."',
	                                                                                '".$origin['title']."-Copy',
	                                                                                ".$origin['owner_gid'].",
	                                                                                ".$origin['owner_uid']."
	                                                                            )");
				
				}
			
                // last inserted pagemap_id
                $newPageID = $this->db->getOne("SELECT last_value FROM pagemap_id_seq");

                // to be sure we fetch correct data and joined attributes 
                $inserted = $this->db->getRow("SELECT pm.*, d.name AS datatype FROM pagemap as pm
                                                LEFT JOIN datatype as d ON (pm.pagetype = d.id) 
                                                WHERE pm.id = " . $newPageID);

                // assign to smarty
                $this->smarty->assign('newPage', $inserted);

                //log action
                $this->log->recordAction("Ajax: Clone page (".$id.")");
                
                $this->r->html = $this->smarty->preFetch('ajax'.DIRECTORY_SEPARATOR.'insertPageRow.html');
    			$this->r->id = $newPageID;
                
            //something went wrong
            }catch(exception $e){
                //log error
                $this->log->recordError($e->getMessage());
            
                //do nothing
            }
        }
    }
?>